import { Body, Controller, Delete, Get, Post, Put } from '@nestjs/common'
import { PrinterService } from './printer.service'
@Controller('printer')
export class PrinterController {
    constructor(private readonly PrinterService: PrinterService) { }

    @Get()
    get() {
        return {
            code: 200,
            msg: "获取成功"
        }
    }

    @Post('addPrinter')
    async addPrinter(@Body() body: any) {
        console.log(body)
        this.PrinterService.addPrinter(
            body
        )
        return {
            code: 200,
            msg: "添加成功"
        }
    }
   
    @Post("getPrinter")
    async getPrinter(@Body() body: any) {
        const { printerName } = body;
        console.log(printerName)
        let searchList = []; 
        if (printerName) {
            searchList.push({ printerName: { $regex: printerName } });
        }


        let data = await this.PrinterService.getPrinter(searchList)

        let data2 = await this.PrinterService.getPrinter([])

        let printerNameList = [{
            value: '',
            label: '请选择',
        }]
        
        data2.forEach(item => {
            printerNameList.push({ value: item.printerName, label: item.printerName})
        })
        return {
            code: 200,
            msg: "查询成功",
            data,
            printerNameList
        }
    } 

    @Put("updatePrinter") 
    async updatePrinter(@Body() body: any) {
        this.PrinterService.updatePrinter(body)
        return {
            code: 200,
            msg: "修改成功" 
        }
    } 

    @Delete("deletePrinter")
    async deletePrinter(@Body() body: any) {
        this.PrinterService.deletePrinter(body)
        return {
            code: 200,
            msg: "删除成功"
        }
    }

}
